<?php
class ControllerNewsNews extends Controller {
	
	public function index (){
		$this->language->load('news/news');
		$this->load->model('news/news');
		$this->load->model('tool/seo_url'); 
		$this->document->title = "Danh Mục Tin Tức";
      	$this->document->keywords = "Tin Tức";
      	$this->document->description = "Danh Mục Tin Tức";
		$News = array();
		if (isset($this->request->get['page'])) 
		{
			$page = $this->request->get['page'];
		} 
		else 
		{ 
			$page = 1;
		}	
		$limit = 7;
		$AllNews = $this->model_news_news->getAllNewsLimit(($page - 1)*$limit,$limit);
		$Total_News = $this->model_news_news->getTotalNews();
		if($AllNews!=null&&$AllNews!=""&&$AllNews!=array())
		{
			foreach($AllNews as $New)
			{
				$News[] = array(
				"new_id" => $New['new_id'],
				"title" => $New['title'],
				"description" => html_entity_decode($New['short_des']), 
				"create_date" => $this->time->makeDays($New['create_date']),
				"image" =>$this->timhinh(html_entity_decode($New['description'])),
				);
				
			}
		}
		$this->data['AllNews'] = $News;
		$pagination = new Pagination();
		$pagination->total = $Total_News;
		$pagination->page = $page;
		$pagination->limit = $limit;
		$pagination->text = "";
		$pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=news/news&page={page}');
		$this->data['pagination'] = $pagination->render();	
		$this->data['link_news'] = "index.php?route=news/detail";
		$this->children = array(
			'common/column_right',
			'common/footer',
			'common/column_left',
			'common/header'
		);
		
	$this->data['heading_title'] = $this->language->get('heading_title');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/news.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/news/news.tpl';
		} else {
			$this->template = 'default/template/news/news.tpl';
		}
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	public function timhinh($text)
	{
		preg_match('/ src=\"(.*?)\"/',$text,$arr);
		if($arr==null)
		{
			$image= "catalog/view/theme/default/image/no_image.jpg";
		}
		else $image = $arr[1];
		return $image;
	}
}